package com.jx.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class ConsumerControl {
    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Value("${spring.application.name}")
    private String appName;
    @Value("${Nacos.toserver}")
    private String toServer;

    @GetMapping(value = "/consumer/getMessageNacos")
    public String getMessageNacos(){
        ServiceInstance serviceInstance = loadBalancerClient.choose(toServer);
        System.out.println("http://"+serviceInstance.getHost()
                +":"+serviceInstance.getPort()+"/getMessageNacos");
        return restTemplate.getForObject("http://"+serviceInstance.getHost()
                        +":"+serviceInstance.getPort()+"/getMessageNacos",String.class);

    }
}
